package com.qst.system.controller;

import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.core.page.TableDataInfo;
import com.qst.system.domain.BankSend;
import com.qst.system.domain.vo.BankSendVo;
import com.qst.system.service.IBankSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/send")
public class BankSendController extends BaseController {

    @Autowired
    private IBankSendService bankSendService;

    @PreAuthorize("@ss.hasPermi('system:send:list')")
    @GetMapping("/list")
    public TableDataInfo list(BankSendVo bankSendVo)
    {
        startPage();
        List<BankSendVo> bankSendList = bankSendService.selectBankSendList(bankSendVo);
        System.out.println(bankSendList);
        return getDataTable(bankSendList);
    }

    @PreAuthorize("@ss.hasPermi('system:send:add')")
    @PostMapping
    public AjaxResult add(@RequestBody BankSendVo bankSendVo){
        return toAjax(bankSendService.insertBankSend(bankSendVo));
    }

    @PreAuthorize("@ss.hasPermi('system:send:edit')")
    @GetMapping("/{id}")
    public AjaxResult selectBankSendById(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bankSendService.selectBankSendById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:send:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody BankSendVo bankSendVo) {
            return toAjax(bankSendService.updateBankSend(bankSendVo));
    }

    @PreAuthorize("@ss.hasPermi('system:send:edit')")
    @PutMapping("/changeSendStatus")
    public AjaxResult changeSendStatus(@RequestBody BankSendVo bankSendVo) {
        return toAjax(bankSendService.updateBankSend(bankSendVo));
    }

    @PreAuthorize("@ss.hasPermi('system:send:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids) {
        try {
            // 处理单个ID情况
            if (!ids.contains(",")) {
                return toAjax(bankSendService.deleteBankSendById(Long.parseLong(ids)));
            }

            // 处理批量ID情况
            String[] idStrs = ids.split(",");
            Long[] idsArr = new Long[idStrs.length];
            for (int i = 0; i < idStrs.length; i++) {
                idsArr[i] = Long.parseLong(idStrs[i].trim());
            }
            return toAjax(bankSendService.deleteBankSendByIds(idsArr));
        } catch (NumberFormatException e) {
            return AjaxResult.error("删除失败，ID格式不正确");
        } catch (Exception e) {
            return AjaxResult.error("删除失败：" + e.getMessage());
        }
    }
}
